$(document).ready(function() {
    ifLoged();
    var status = "",
        statusUnion = ['', '待付款', '预定中', '已完成', '已取消'],
        startTime = "",
        endTime = "",
        timestamp;
    // 如果全部订单激活
    if (page === undefined) {
        var page = 1;
    }
    fillData.init('pageArray', url + "v1/order/list", function(data) {
        fillIndentData(data);
    }, {
        token: localStorage.APISHoteltoken,
        limit: 6,
        page: page,
    }, {
        maxpage: "orderList",
    });
    laydate.render({
        elem: '#timeRange', //指定元素
        range: '至',
        format: 'yyyy-MM-dd',
        min: 0, //
        max: 30, //30天后
        done: function(date, fromDate, endDate) {
            var strS = fromDate.year + '/' + fromDate.month + '/' + fromDate.date,
                strE = endDate.year + '/' + endDate.month + '/' + endDate.date,
                dateStart = new Date(strS),
                dateEnd = new Date(strE);
            console.log(strS);
            console.log(strE);
            startTime = dateStart.getTime().toString();
            endTime = dateEnd.getTime().toString();
            timestamp = startTime + ',' + endTime;
        }
    });
    $('.intentSort').each(function(index) {
        var $this = $(this);
        $this.on('click', function() {
            status = statusUnion[index];
            if (!$this.hasClass("active")) {
                $(this).addClass("active").siblings().removeClass("active");
                fillData.init('pageArray', url + "v1/order/list", function(data) {
                    fillIndentData(data);
                }, {
                    token: localStorage.APISHoteltoken,
                    limit: 6,
                    page: page,
                    status: statusUnion[index],
                    orderNo: $('#searchOrderId').val(),
                    customerName: $('#searchPassenger').val(),
                    checkTime: timestamp
                }, {
                    maxpage: "orderList",
                });
            }
        });

    })

    $('.search').on('click', function() {
        if (!$('#timeRange').val()) {
            fillData.init('pageArray', url + "v1/order/list", function(data) {
                fillIndentData(data);
            }, {
                token: localStorage.APISHoteltoken,
                limit: 6,
                page: page,
                status: status,
                orderNo: $('#searchOrderId').val(),
                customerName: $('#searchPassenger').val(),
                checkTime: timestamp
            }, {
                maxpage: "orderList",
            })
        } else {
            fillData.init('pageArray', url + "v1/order/list", function(data) {
                fillIndentData(data);
            }, {
                token: localStorage.APISHoteltoken,
                limit: 6,
                page: page,
                status: status,
                orderNo: $('#searchOrderId').val(),
                customerName: $('#searchPassenger').val(),
            }, {
                maxpage: "orderList",
            })
        }
    })



    function fillIndentData(data) {
        if (data.code != "success") {
            $('.noindent').html(data.message);
            $('.noindent').show();
        } else {
            if (data.count != 0) {
                console.log("获取订单信息成功"); // 提示用户
            } else {
                $('.noindent').show();
            }
            $('.infoTable tbody').html('');
            var orderList = data.data.orderList;
            var orderItem = '<tr>';
            for (let i = 0; i < orderList.length; i++) {
                const o = orderList[i];
                orderItem += '<td class="orderNo">' + o.orderNo + '</td>';
                orderItem += ' <td class="name"> <p class = "hotelName" > ' + o.hotel.name + ' </p><p class="roomName">' + o.roomName + ' </p></td>'
                orderItem += '<td class="orderTime">' + moment(o.create_time).format('YYYY-MM-DD') + '</td>';
                orderItem += '<td class="passenger">' + o.customerName + '</td>';
                orderItem += '<td class="amount">￥' + o.amount + '</td>'
                orderItem += '<td class="orderStatus">' + o.status + '</td>';

                var a = JSON.stringify(o),
                    b = encodeURIComponent(a);
                //将所有数据转码url传递
                if (o.status == "预定中") {
                    orderItem += '<td class="operation"><a href="indentDetail.html?id=' + o._id + '">查看</a> <a href="cancelIndent.html?id=' + o._id + '">取消预定</a></td></tr>'
                } else if (o.status == "待支付") {
                    orderItem += '<td class="operation"><a href="indentDetail.html?id=' + o._id + '">查看</a> <a href="billings.html?' + b + '">去支付</a></td></tr>'
                } else if (o.status == "已取消") {
                    orderItem += '<td class="operation"><a href="indentDetail.html?id=' + o._id + '">查看</a></td></tr>';
                } else if (o.status == "已完成") {
                    orderItem += '<td class="operation"><a href="indentDetail.html?id=' + o._id + '">查看</a><a href="submitComment.html?' + b + '">去评价</a></td></tr>';
                } else {
                    orderItem += '<td class="operation"><a href="">请刷新重试！</a></td></tr>';
                }
            }
            $('.infoTable tbody').append(orderItem);
        }
    }
})